﻿<%@ Page Language="C#" MasterPageFile="~/MasterPages/AdminDefault.Master" AutoEventWireup="true"
  CodeBehind="NewsLetterList.aspx.cs" Inherits="ITViet.Web.Admin.NewsLetterList"
  Title="" %>

<%@ Register Assembly="DevExpress.Web.v9.1, Version=9.1.4.0, Culture=neutral, PublicKeyToken=5377c8e3b72b4073"
  Namespace="DevExpress.Web.ASPxUploadControl" TagPrefix="dxuc" %>
<%@ Register Assembly="DevExpress.Web.v9.1, Version=9.1.4.0, Culture=neutral, PublicKeyToken=5377c8e3b72b4073"
  Namespace="DevExpress.Web.ASPxCallback" TagPrefix="dxcb" %>
<%@ Register Assembly="DevExpress.Web.v9.1, Version=9.1.4.0, Culture=neutral, PublicKeyToken=5377c8e3b72b4073"
  Namespace="DevExpress.Web.ASPxPopupControl" TagPrefix="dxpc" %>
<%@ Register Assembly="DevExpress.Web.ASPxHtmlEditor.v9.1, Version=9.1.4.0, Culture=neutral, PublicKeyToken=5377c8e3b72b4073"
  Namespace="DevExpress.Web.ASPxHtmlEditor" TagPrefix="dxhe" %>
<%@ Register Assembly="DevExpress.Web.v9.1, Version=9.1.4.0, Culture=neutral, PublicKeyToken=5377c8e3b72b4073"
  Namespace="DevExpress.Web.ASPxTabControl" TagPrefix="dxtc" %>
<%@ Register Assembly="DevExpress.Web.ASPxGridView.v9.1, Version=9.1.4.0, Culture=neutral, PublicKeyToken=5377c8e3b72b4073"
  Namespace="DevExpress.Web.ASPxGridView" TagPrefix="dxwgv" %>
<%@ Register Assembly="DevExpress.Web.ASPxEditors.v9.1, Version=9.1.4.0, Culture=neutral, PublicKeyToken=5377c8e3b72b4073"
  Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dxe" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
  <link rel="Stylesheet" type="text/css" href="../Styles/Css/swfUpload.css" />
  <link href="../Css/jquery.autocomplete.css" rel="stylesheet" type="text/css" />

  <script src="../Scripts/jquery-1.4.3.min.js" type="text/javascript"></script>

  <script type="text/javascript" src="../Scripts/swfupload.js"></script>

  <script type="text/javascript" src="../Scripts/swfupload.queue.js"></script>

  <script type="text/javascript" src="../Scripts/fileprogress.js"></script>

  <script type="text/javascript" src="../Scripts/handlers.js"></script>

  <script src="../Scripts/jquery.autocomplete.js" type="text/javascript"></script>

  <script type="text/javascript">
    var uploader;
    var completeFunction = '<%=UploadCompleteFunction %>';
    var uploadSessionName = '<%=UploadSessionName %>';
    var uploadFolder = '<%=UploadFolder%>';
    window.onload = function() {
      uploader = new SWFUpload({
        // Backend Settings
        upload_url: "UploadPage.aspx",
        post_params: {
          "ASPSESSID": "<%=Session.SessionID %>"
        },

        // File Upload Settings
        file_size_limit: "10 MB",
        file_types: "*.*",
        file_types_description: "",
        file_upload_limit: 0,    // Zero means unlimited
        file_queue_limit: 0,
        file_post_name: uploadSessionName,

        // Event Handler Settings - these functions as defined in Handlers.js
        //  The handlers are not part of SWFUpload but are part of my website and control how
        //  my website reacts to the SWFUpload events.
        swfupload_preload_handler: preLoad,
        swfupload_load_failed_handler: loadFailed,
        file_queue_error_handler: fileQueueError,
        file_dialog_complete_handler: fileDialogComplete,
        upload_start_handler: uploadStart,
        upload_progress_handler: uploadProgress,
        upload_error_handler: uploadError,
        upload_success_handler: uploadSuccess,
        upload_complete_handler: uploadComplete,

        // Button settings
        button_image_url: "",
        button_placeholder_id: "spanButtonPlaceholder",
        button_width: 90,
        button_height: 22,
        button_text: '<span class="button"><%=(string)GetGlobalResourceObject("Strings","AttachAFile") %></span>',
        button_text_style: '.button { font-family: Tahoma; font-size: 12pt;text-decoration:underline;color:#60A4A8;}',
        button_text_top_padding: 1,
        button_text_left_padding: 2,
        button_cursor: SWFUpload.CURSOR.HAND,

        // Flash Settings
        flash_url: "../Styles/Swf/swfupload.swf", // Relative to this file
        flash9_url: "../Styles/Swf/swfupload_FP9.swf", // Relative to this file

        custom_settings: {
          finishedUpload: completeFunction
        },

        // Debug Settings
        debug: false
      });
    }
  </script>

  <div id="frmEmailList" style="padding-top: 10px;">
    <div style="padding-bottom: 20px;">
      <dxe:ASPxHyperLink ID="ASPxHyperLink1" runat="server" Text="<%$Resources:Strings, Compose %>"
        CssClass="LinkItem">
        <ClientSideEvents Click="function(s,e){showCompose(true);e.processOnServer=false;}" />
      </dxe:ASPxHyperLink>
    </div>
    <dxwgv:ASPxGridView ID="gvList" runat="server" AutoGenerateColumns="False" ClientInstanceName="gvList"
      KeyFieldName="Id" OnCustomCallback="gvList_CustomCallback" OnCustomJSProperties="gvList_CustomJSProperties"
      OnRowDeleting="gvList_RowDeleting" OnRowInserting="gvList_RowInserting" OnRowUpdating="gvList_RowUpdating"
      Width="100%">
      <SettingsBehavior ConfirmDelete="True" AllowSort="False" />
      <SettingsText ConfirmDelete="<%$ Resources:Messages,ERR0006 %>"></SettingsText>
      <ClientSideEvents EndCallback="function(s, e){ongvList_EndCallback(s,e);}"></ClientSideEvents>
      <Columns>
        <dxwgv:GridViewCommandColumn VisibleIndex="0" Width="60px" ButtonType="Image" Caption=" ">
          <DeleteButton Visible="True">
            <Image Url="~/Images/delete.png" />
          </DeleteButton>
          <EditButton Visible="True">
            <Image Url="~/Images/edit.png" />
          </EditButton>
          <NewButton Visible="True">
            <Image Url="~/Images/add.png" />
          </NewButton>
          <CancelButton>
            <Image Url="~/Images/undo.gif" />
          </CancelButton>
          <UpdateButton>
            <Image Url="~/Images/save.png" />
          </UpdateButton>
          <HeaderStyle HorizontalAlign="Center" />
          <CellStyle Cursor="pointer">
          </CellStyle>
        </dxwgv:GridViewCommandColumn>
        <dxwgv:GridViewDataTextColumn VisibleIndex="1" Caption="<%$ Resources:Strings, Email %>"
          FieldName="Email" SortIndex="0" SortOrder="Ascending">
          <EditFormSettings VisibleIndex="0" />
          <HeaderStyle HorizontalAlign="Center" />
        </dxwgv:GridViewDataTextColumn>
        <dxwgv:GridViewDataTextColumn VisibleIndex="2" Caption="<%$ Resources:Strings, Name %>"
          FieldName="CustomerName">
          <PropertiesTextEdit DisplayFormatString="{0}">
          </PropertiesTextEdit>
          <EditFormSettings Visible="True" VisibleIndex="1" />
          <HeaderStyle HorizontalAlign="Center" />
        </dxwgv:GridViewDataTextColumn>
        <dxwgv:GridViewDataMemoColumn FieldName="Note" VisibleIndex="3" Caption="<%$Resources:Strings, Note %>">
          <PropertiesMemoEdit DisplayFormatString="d">
          </PropertiesMemoEdit>
          <EditFormSettings VisibleIndex="2" />
          <EditFormSettings VisibleIndex="2"></EditFormSettings>
          <HeaderStyle HorizontalAlign="Center" />
        </dxwgv:GridViewDataMemoColumn>
      </Columns>
      <SettingsText ConfirmDelete="<%$Resources:Messages,ERR0006 %>" />
      <ClientSideEvents EndCallback="function(s, e){ongvList_EndCallback(s,e);}" />
    </dxwgv:ASPxGridView>
  </div>
  <div id="frmCompose" style="padding-top: 10px; display: none;">
    <table class="CommonFont" width="100%">
      <tr>
        <td style="width: 70px;">
          <span onclick="setEmailType('TO');showEmailList();return false;" style="cursor: pointer;">
            <asp:Label ID="Label1" runat="server" Text="<%$ Resources:Strings, To %>" CssClass="LinkItem"></asp:Label>
            : </span>
        </td>
        <td>
          <input id="txtTo" type="text" style="width: 99%; height: 15px; padding: 2px;" class="dxeTextBox_Aqua"
            runat="server" />
        </td>
      </tr>
      <tr>
        <td>
          <span onclick="setEmailType('CC');showEmailList();return false;" style="cursor: pointer;">
            <asp:Label ID="Label2" runat="server" Text="Cc" CssClass="LinkItem"></asp:Label>
            : </span>
        </td>
        <td>
          <input id="txtCC" type="text" style="width: 99%; height: 15px; padding: 2px;" class="dxeTextBox_Aqua"
            runat="server" />
        </td>
      </tr>
      <tr>
        <td>
          <span onclick="setEmailType('BCC');showEmailList();return false;" style="cursor: pointer;">
            <asp:Label ID="Label3" runat="server" Text="Bcc" CssClass="LinkItem"></asp:Label>
            : </span>
        </td>
        <td>
          <input id="txtBCC" type="text" style="width: 99%; height: 15px; padding: 2px;" class="dxeTextBox_Aqua"
            runat="server" />
        </td>
      </tr>
      <tr>
        <td>
          <asp:Label ID="Label4" runat="server" Text="<%$ Resources:Strings, Subject %>" Font-Bold="True"></asp:Label>
          :
        </td>
        <td>
          <dxe:ASPxTextBox ID="txtSubject" ClientInstanceName="txtSubject" runat="server" Width="100%">
          </dxe:ASPxTextBox>
        </td>
      </tr>
      <tr>
        <td>
        </td>
        <td style="padding-top: 4px;">
          <div style="float: left">
            <asp:Image ID="Image1" ImageUrl="~/Images/attachment.png" runat="server" />
          </div>
          <div style="float: left">
          </div>
          <div id="content" style="float: left; padding-left: 0px;">
            <div id="swfu_container" style="margin: 0px 10px;">
              <span id="spanButtonPlaceholder" style="cursor: pointer;"></span>
            </div>
          </div>
          <div style="padding-left: 20px;">
            <dxwgv:ASPxGridView ID="gvFile" runat="server" AutoGenerateColumns="False" ClientInstanceName="gvFile"
              KeyFieldName="FilePath" OnCustomCallback="gvFile_CustomCallback" OnRowDeleting="gvFile_RowDeleting"
              ClientVisible="False" Border-BorderStyle="Solid" Width="520px">
              <Images>
                <CollapsedButton Width="11px" />
                <DetailCollapsedButton Width="9px" />
                <HeaderFilter Width="18px" />
                <HeaderActiveFilter Width="18px" />
                <FilterRowButton Width="13px" />
                <CustomizationWindowClose Width="17px" />
                <PopupEditFormWindowClose Width="17px" />
                <FilterBuilderClose Width="17px" />
              </Images>
              <ClientSideEvents EndCallback="function(s,e){ongvFile_EndCallback(s,e);}"></ClientSideEvents>
              <Columns>
                <dxwgv:GridViewDataTextColumn FieldName="FileName" VisibleIndex="1">
                </dxwgv:GridViewDataTextColumn>
                <dxwgv:GridViewCommandColumn ButtonType="Image" VisibleIndex="0" Width="20px">
                  <DeleteButton Visible="True">
                    <Image Url="~/Images/delete.png"></Image>
                  </DeleteButton>
                  <CellStyle Cursor="pointer">
                    <Paddings PaddingLeft="3px"></Paddings>
                  </CellStyle>
                </dxwgv:GridViewCommandColumn>
                <dxwgv:GridViewDataTextColumn FieldName="FilePath" Visible="False" VisibleIndex="2">
                </dxwgv:GridViewDataTextColumn>
              </Columns>
              <Settings ShowColumnHeaders="False" />
              <SettingsPager Mode="ShowAllRecords">
              </SettingsPager>
              <ClientSideEvents EndCallback="function(s,e){ongvFile_EndCallback(s,e);}" />
              <Settings ShowColumnHeaders="False"></Settings>
            </dxwgv:ASPxGridView>
          </div>
        </td>
      </tr>
    </table>
    <div style="padding-top: 5px;">
      <dxhe:ASPxHtmlEditor ID="htmlEditor" ClientInstanceName="htmlEditor" runat="server">
        <SettingsImageUpload>
          <ValidationSettings AllowedContentTypes="image/jpeg,image/pjpeg,image/gif,image/png,image/x-png">
          </ValidationSettings>
        </SettingsImageUpload>
      </dxhe:ASPxHtmlEditor>
    </div>
    <div style="padding-top: 10px; float: left;">
      <dxe:ASPxButton ID="btnSend" ClientInstanceName="btnSend" runat="server" Text="<%$ Resources:Strings, Send %>">
        <ClientSideEvents Click="function(s,e){sendMail();e.processOnServer=false;}" />
      </dxe:ASPxButton>
    </div>
    <div style="padding-top: 10px; float: left; padding-left: 3px;">
      <dxe:ASPxButton ID="ASPxButton3" ClientInstanceName="btnSend" runat="server" Text="<%$ Resources:Strings, Close %>">
        <ClientSideEvents Click="function(s,e){showCompose(false);e.processOnServer=false;}" />
      </dxe:ASPxButton>
    </div>
  </div>
  <div>
    <dxpc:ASPxPopupControl ID="pcEmailList" ClientInstanceName="pcEmailList" runat="server"
      Modal="True" PopupHorizontalAlign="WindowCenter" PopupVerticalAlign="WindowCenter"
      AllowDragging="True" CloseAction="CloseButton" Width="500px" HeaderText="<%$ Resources:Strings, Email %>">
      <ContentCollection>
        <dxpc:PopupControlContentControl>
          <div style="padding: 20px;">
            <dxwgv:ASPxGridView ID="gvSelect" runat="server" AutoGenerateColumns="False" ClientInstanceName="gvSelect"
              KeyFieldName="Id" OnCustomCallback="gvSelect_CustomCallback" Width="100%">
              <SettingsBehavior ConfirmDelete="True" AllowSort="False" />
              <Columns>
                <dxwgv:GridViewCommandColumn VisibleIndex="0" Width="20px" ButtonType="Image" ShowSelectCheckbox="True">
                  <HeaderStyle HorizontalAlign="Center" Cursor="pointer" />
                  <HeaderTemplate>
                    <input id="Checkbox1" type="checkbox" style="cursor: pointer;" onclick="gvSelect.SelectAllRowsOnPage(this.checked)" />
                  </HeaderTemplate>
                  <CellStyle Cursor="pointer">
                  </CellStyle>
                </dxwgv:GridViewCommandColumn>
                <dxwgv:GridViewDataTextColumn VisibleIndex="1" Caption="<%$ Resources:Strings, Email %>"
                  FieldName="Email" SortIndex="0" SortOrder="Ascending">
                  <EditFormSettings VisibleIndex="0" />
                  <HeaderStyle HorizontalAlign="Center" />
                </dxwgv:GridViewDataTextColumn>
                <dxwgv:GridViewDataTextColumn VisibleIndex="2" Caption="<%$ Resources:Strings, Name %>"
                  FieldName="CustomerName">
                  <PropertiesTextEdit DisplayFormatString="{0}">
                  </PropertiesTextEdit>
                  <EditFormSettings Visible="True" VisibleIndex="1" />
                  <HeaderStyle HorizontalAlign="Center" />
                </dxwgv:GridViewDataTextColumn>
              </Columns>
              <SettingsText ConfirmDelete="<%$Resources:Messages,ERR0006 %>" />
              <ClientSideEvents EndCallback="function(s, e){ongvSelect_EndCallback(s,e);}" />
            </dxwgv:ASPxGridView>
            <div style="float: right; padding: 7px 0px 20px 0px;">
              <dxe:ASPxButton ID="ASPxButton2" runat="server" Text="<%$Resources:Strings, Close %>">
                <ClientSideEvents Click="function(s,e){onCancel();e.processOnServer=false;}" />
              </dxe:ASPxButton>
            </div>
            <div style="float: right; padding: 7px 3px 20px 0px;">
              <dxe:ASPxButton ID="ASPxButton1" runat="server" Text="<%$Resources:Strings, Select %>">
                <ClientSideEvents Click="function(s,e){onSelect();e.processOnServer=false;}" />
              </dxe:ASPxButton>
            </div>
          </div>
        </dxpc:PopupControlContentControl>
      </ContentCollection>
    </dxpc:ASPxPopupControl>
  </div>
  <div>
    <dxcb:ASPxCallback ID="ctrlCallback" ClientInstanceName="ctrlCallback" runat="server"
      OnCallback="ctrlCallback_Callback">
      <ClientSideEvents CallbackComplete="function(s,e){onctrlCallback_CallbackComplete(s,e);}" />
    </dxcb:ASPxCallback>
    <asp:HiddenField ID="hfEmailType" runat="server" />
  </div>

  <script type="text/javascript" language="javascript">
    function showFlash(show) {
      var flash = document.getElementById('swfu_container');
      if (show)
        flash.style.display = 'block';
      else
        flash.style.display = 'none';
    }

    function setEmailType(type) {
      document.getElementById('<%=hfEmailType.ClientID %>').value = type;
    }

    function ongvList_EndCallback(s, e) {
      var error = gvList.cpInnerError;
      if (error != '')
        alertMessage(error);
    }

    function showCompose(show) {
      var frmEmail = document.getElementById('frmEmailList');
      var frmCompose = document.getElementById('frmCompose');
      if (show) {
        resetValue();
        frmEmail.style.display = 'none';
        frmCompose.style.display = 'block';
        showFlash(show);
        htmlEditor.AdjustControl();
      }
      else {
        frmEmail.style.display = 'block';
        frmCompose.style.display = 'none';
      }
    }

    function onTabChange(s, e) {
      var tabName = e.tab.name;
      if (tabName == "Compose") {
        resetValue();
        showCompose(true);
      }
      else
        showCompose(false);
    }

    function showEmailList() {
      //resetValue();
      gvSelect.PerformCallback('Load');
    }

    function ongvSelect_EndCallback(s, e) {
      gvSelect.SelectAllRowsOnPage(false);
      showFlash(false);
      pcEmailList.Show();
    }

    function resetValue() {
      var txtTo = document.getElementById('<%=txtTo.ClientID %>');
      var txtCC = document.getElementById('<%=txtCC.ClientID %>');
      var txtBCC = document.getElementById('<%=txtBCC.ClientID %>');
      gvSelect.SelectAllRowsOnPage(false);
      txtTo.value = '';
      txtCC.value = '';
      txtBCC.value = '';
      txtSubject.SetText('');
      htmlEditor.SetHtml('');

      //Remove all attchment
      for (var i = 0; i < gvFile.GetVisibleRowsOnPage(); i++) {
        gvFile.DeleteRow(i);
      }
      gvFile.SetVisible(false);
    }

    function ongvFile_EndCallback(s, e) {
      if (s.GetVisibleRowsOnPage() > 0)
        s.SetVisible(true);
      else
        s.SetVisible(false);
    }

    function attachFile() {
      gvFile.PerformCallback('Save|');
    }

    function onSelect() {
      gvSelect.GetSelectedFieldValues('Email', onGetSelectedEmail);
    }

    function onGetSelectedEmail(selectedValues) {
      var txtTo = document.getElementById('<%=txtTo.ClientID %>');
      var txtCC = document.getElementById('<%=txtCC.ClientID %>');
      var txtBCC = document.getElementById('<%=txtBCC.ClientID %>');
      var emailList = '';
      for (var i = 0; i < selectedValues.length; i++) {
        emailList += selectedValues[i] + ';';
      }

      var type = document.getElementById('<%=hfEmailType.ClientID %>').value;
      switch (type) {
        case 'TO':
          txtTo.value += emailList;
          break;
        case 'CC':
          txtCC.value += emailList;
          break;
        case 'BCC':
          txtBCC.value += emailList;
          break;
      }
      pcEmailList.Hide();
      showFlash(true);
    }

    function onCancel() {
      pcEmailList.Hide();
      showFlash(true);
    }

    function sendMail() {
      if (checkSendMailInput()) {
        pcWaiting.Show();
        ctrlCallback.SendCallback();
      }
    }

    function onctrlCallback_CallbackComplete(s, e) {
      pcWaiting.Hide();
      if (e.result != '') {
        alertMessage(e.result);
        return;
      }
      resetValue();
      alertMessage('<%=(string)GetGlobalResourceObject("Messages", "ERR0033") %>');
    }

    function onFileSelect() {
      if (uploader.GetText() == '')
        return;
      document.getElementById('frmUpload').style.display = 'none';

    }

    function checkSendMailInput() {
      var txtTo = document.getElementById('<%=txtTo.ClientID %>');
      var txtCC = document.getElementById('<%=txtCC.ClientID %>');
      var txtBCC = document.getElementById('<%=txtBCC.ClientID %>');
      var error = '';
      if (txtTo.value == '') {
        error += '<BR> + <%=(string)GetGlobalResourceObject("Strings","To") %>';
        error = '<%=(string)GetGlobalResourceObject("Messages", "ERR0008") %>' + error;
      }

      if (error != '') {
        alertMessage(error);
        return false;
      }

      error = '<%=(string)GetGlobalResourceObject("Messages","ERR0031") %>';
      if (!validateEmailList(txtTo.value)) {
        alertMessage(error);
        return false;
      }

      if (!validateEmailList(txtCC.value)) {
        alertMessage(error);
        return false;
      }

      if (!validateEmailList(txtBCC.value)) {
        alertMessage(error);
        return false;
      }

      return true;
    }

    function validateEmailList(emailString) {
      var emailList = emailString.split(';');
      var isValid = true;
      if (emailList != null)
        for (var i = 0; i < emailList.length; i++) {
        if (emailList[i] == '')
          continue;
        if (!ValidateEmail(emailList[i])) {
          isValid = false;
          break;
        }
      }
      return isValid;
    }

    function loadAutoCom(obj, data) {
      obj.autocomplete(data, {
        width: 300,
        scroll: false,
        minChars: 0,
        mustMatch: false,
        matchContains: true,
        multiple: true,
        multipleSeparator: ";",
        autoFill: false,
        formatItem: function(row, i, total, term) {
          return row.Email;
        },
        formatMatch: function(row, i, total, term) {
          return row.Email;
        },
        formatResult: function(row, i, total) {
          return row.Email;
        }
      });
    }

    jQuery(document).ready(function() {
      var data = <%=ListNewsLetter %>;
      loadAutoCom(jQuery("#" + '<%=txtTo.ClientID %>'), data);
      loadAutoCom(jQuery("#" + '<%=txtCC.ClientID %>'), data);
      loadAutoCom(jQuery("#" + '<%=txtBCC.ClientID %>'), data);
    });
  </script>

</asp:Content>
